//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SimpleQuote.h"
using namespace Cephei::QL::Quotes;
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (Microsoft::FSharp::Core::FSharpOption<Double>^ value) : CQuote(CSimpleQuote::typeid)
{
    try
    {
#ifdef HANDLE
        _phSimpleQuote = NULL;
#endif
        QuantLib::Real _value = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (value) ? (QuantLib::Real)ValueHelper::Convert (value->Value) : Null<QuantLib::Real>()); //4
        _ppSimpleQuote = new boost::shared_ptr<QuantLib::SimpleQuote> (new QuantLib::SimpleQuote ( _value ));
        SetQuote (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppSimpleQuote));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (boost::shared_ptr<QuantLib::SimpleQuote>& childNative, Object^ owner) : CQuote(CSimpleQuote::typeid)
{
#ifdef HANDLE
	_phSimpleQuote = NULL;
#endif
	_ppSimpleQuote = &childNative;
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppSimpleQuote));
}
Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (QuantLib::SimpleQuote& childNative, Object^ owner) : CQuote(CSimpleQuote::typeid)
{
#ifdef HANDLE
	_phSimpleQuote = NULL;
#endif
	_ppSimpleQuote = new boost::shared_ptr<QuantLib::SimpleQuote> (&childNative);
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppSimpleQuote));
    _SimpleQuoteOwner = owner;
    _QuoteOwner = owner;
}

Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (CSimpleQuote^ copy) : CQuote(CSimpleQuote::typeid)
{
#ifdef HANDLE
	_phSimpleQuote = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSimpleQuote = new boost::shared_ptr<QuantLib::SimpleQuote> (copy->GetShared());
        _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppSimpleQuote));
    }
}
Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (PLATFORM::Type^ t) : CQuote(CSimpleQuote::typeid)
{
#ifdef HANDLE
	_phSimpleQuote = NULL;
#endif
	if (!t->IsSubclassOf(CSimpleQuote::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (QuantLib::Handle<QuantLib::SimpleQuote>& childNative, Object^ owner)  : CQuote(CSimpleQuote::typeid)
{
	_phSimpleQuote = &childNative;
	_ppSimpleQuote = &static_cast<boost::shared_ptr<QuantLib::SimpleQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppSimpleQuote));
    _SimpleQuoteOwner = owner;
}
Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (QuantLib::Handle<QuantLib::SimpleQuote> childNative)  : CQuote(CSimpleQuote::typeid)
{
	_phSimpleQuote = &childNative;
	_ppSimpleQuote = &static_cast<boost::shared_ptr<QuantLib::SimpleQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppSimpleQuote));
}
#endif
#ifdef STRUCT
Cephei::QL::Quotes::CSimpleQuote::CSimpleQuote (QuantLib::SimpleQuote childNative)  : CQuote(CSimpleQuote::typeid)
{
#ifdef HANDLE
	_phSimpleQuote = NULL;
#endif
	_ppSimpleQuote = new boost::shared_ptr<QuantLib::SimpleQuote> (new QuantLib::SimpleQuote (childNative));
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppSimpleQuote));
}
#endif

Cephei::QL::Quotes::CSimpleQuote::~CSimpleQuote ()
{
    if (_ppSimpleQuote != NULL)
    {
	    delete _ppSimpleQuote;
        _ppSimpleQuote = NULL;
    }
}
Cephei::QL::Quotes::CSimpleQuote::!CSimpleQuote ()
{
    if (_ppSimpleQuote != NULL)
    {
	    delete _ppSimpleQuote;
    }
}
QuantLib::SimpleQuote& Cephei::QL::Quotes::CSimpleQuote::GetReference ()
{
    if (_ppSimpleQuote == NULL) throw REFNEW NativeNullException ();
	return **_ppSimpleQuote;
}
boost::shared_ptr<QuantLib::SimpleQuote>& Cephei::QL::Quotes::CSimpleQuote::GetShared ()
{
    if (_ppSimpleQuote == NULL) throw REFNEW NativeNullException ();
	return *_ppSimpleQuote;
}
QuantLib::SimpleQuote* Cephei::QL::Quotes::CSimpleQuote::GetPointer ()
{
    if (_ppSimpleQuote == NULL) throw REFNEW NativeNullException ();
	return &**_ppSimpleQuote;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SimpleQuote>& Cephei::QL::Quotes::CSimpleQuote::GetHandle ()
{
	if (_phSimpleQuote == NULL)
	{
		_phSimpleQuote = new Handle<QuantLib::SimpleQuote> (*_ppSimpleQuote);
	}
	return *_phSimpleQuote;
}
#endif
bool Cephei::QL::Quotes::CSimpleQuote::HasNative () 
{
	return (_ppSimpleQuote != NULL);
}

Boolean Cephei::QL::Quotes::CSimpleQuote::IsValid::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppSimpleQuote)->isValid ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Quotes::ISimpleQuote^ Cephei::QL::Quotes::CSimpleQuote::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppSimpleQuote)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Quotes::CSimpleQuote::SetValue (Microsoft::FSharp::Core::FSharpOption<Double>^ value)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _value = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (value) ? (QuantLib::Real)ValueHelper::Convert (value->Value) : Null<QuantLib::Real>()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSimpleQuote)->setValue ( _value );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Quotes::CSimpleQuote::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSimpleQuote)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Quotes::ISimpleQuote^ Cephei::QL::Quotes::CSimpleQuote_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ value)
{
    return REFNEW CSimpleQuote ( value);
}
